package com.yanqu.road.server.manger.cricket.skillhandler.startround.handler;

import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.startround.CricketSkillStartRoundStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill167Strategy implements CricketSkillStartRoundStrategy {

    /**
     * 技能处理-生死契约
     * @param cricketGame 本局对局对象
     * @param skillData 技能数据
     * @param action 我方
     * @param target 对方
     * @param roundId 第几回合
     * @param attackRoundBuilder 返回PB结构体
     * @return
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, BattleData action, BattleData target, int roundId, CricketProto.CricketAttackRound.Builder attackRoundBuilder) {
        //回合开始时，生命低于{0}%时，交换双方生命，只能触发{1}次
        if (skillData.getEffectAmount() < skillData.getSkillEffect().getP2()) {
            if (action.getHpRatio() < skillData.getSkillEffect().getP1()) {
                skillData.effectAmountInc();
                long actionHp = action.getHp();
                long targetHp = target.getHp();
                long actionHpDiff = 0;
                long targetHpDiff = 0;
                if (targetHp <= action.getMaxHp()) {
                    action.setHp(targetHp);
                } else {
                    action.setHp(action.getMaxHp());
                }
                actionHpDiff = action.getHp() - actionHp;
                if (actionHp <= target.getMaxHp()) {
                    target.setHp(actionHp);
                } else {
                    target.setHp(target.getMaxHp());
                }
                targetHpDiff = target.getHp() - targetHp;
                CricketProto.CricketBattleFrame.Builder newBattleFrameBuilder1 = cricketGame.newBattleFrameBuilder();
                newBattleFrameBuilder1.setActionUserId(action.getUserId());
                newBattleFrameBuilder1.setTargetUserId(target.getUserId());
                newBattleFrameBuilder1.setSkillId(skillData.getSkillId());
                newBattleFrameBuilder1.setAttack(targetHpDiff);
                cricketGame.fillRemainHp(newBattleFrameBuilder1);
                attackRoundBuilder.addStartRound(newBattleFrameBuilder1);
            }
        }
        return false;
    }
}
